package Service;

import QQcommon.Massage_2;
import QQcommon.Message;
import QQcommon.User_qq;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public class Service {
    private ServerSocket ss=null;
    private static ConcurrentHashMap<String,User_qq> concurrentHashMap=new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, ArrayList<Message>> concurrentHashMap1=new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, ArrayList<Message>> concurrentHashMap2=new ConcurrentHashMap<>();
    static {
        concurrentHashMap.put("100",new User_qq("100","123456"));
        concurrentHashMap.put("10",new User_qq("10","456789"));
        concurrentHashMap.put("11",new User_qq("11","123456"));
    }
    public static boolean checkUser(String uid,String pwd){
        if(concurrentHashMap.get(uid).getUserId()==null){
            return false;
        }
        if(!concurrentHashMap.get(uid).getPwd().equals(pwd)){
            return false;
        }
        return true;
    }
    public static void setConcurrentHashMap1(String uid,ArrayList<Message> arrayList){
        concurrentHashMap1.put(uid,arrayList);
    }
    public static void send(String uid){
        Iterator<String> iterator=concurrentHashMap1.keySet().iterator();
        while (iterator.hasNext()){
            String Uid=iterator.next().toString();
        if (uid.equals(Uid)) {
            ObjectOutputStream oos = null;
            ArrayList<Message> arrayList=concurrentHashMap1.get(Uid);
            Message message=arrayList.get(0);
            try {
                oos = new ObjectOutputStream(Massage_Thread.service_thread(uid).getSocket().getOutputStream());
                oos.writeObject(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
            arrayList.remove(0);
            concurrentHashMap1.remove(Uid);
        }
    }
    }
    public static void sendFile(String uid){
        Iterator<String> iterator=concurrentHashMap2.keySet().iterator();
        while (iterator.hasNext()){
            String Uid=iterator.next().toString();
            if (uid.equals(Uid)) {
                ObjectOutputStream oos = null;
                ArrayList<Message> arrayList=concurrentHashMap2.get(Uid);
                Message message=arrayList.get(0);
                try {
                    oos = new ObjectOutputStream(Massage_Thread.service_thread(uid).getSocket().getOutputStream());
                    oos.writeObject(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                arrayList.remove(0);
                concurrentHashMap2.remove(Uid);
            }
        }
    }
    public Service(){
        try {
            System.out.println("服务器在9999端口监听.....");
            new Thread(new Service_SendNews()).start();
            ss=new ServerSocket(9999);
            while (true){
                Socket socket=ss.accept();
                //得到socket关联的对象输入流
                ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
                //得到socket关联的对象输出流
                ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                User_qq user=(User_qq) ois.readObject();//获取用户端发送的User对象
                //创建一个Message对象，准备回复客户端
                Message message=new Message();
                if (checkUser(user.getUserId(), user.getPwd())){
                    message.setMassageType(Massage_2.Massage_LogIn_success);
                    //将meeage对象回复给客户端
                    oos.writeObject(message);
                    Service_Thread service_thread=new Service_Thread(socket, user.getUserId());
                    Massage_Thread.Add_Thread(user.getUserId(),service_thread);
                    service_thread.start();
                    //把该对象放入集合中
                    Service.send(user.getUserId());
                    Service.sendFile(user.getUserId());
                }else {
                    socket.close();//关闭流
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}